<template>
	<view>
		<u-cell-group title="设备信息">
			<u-cell-item title="设备名称" :value="deviceData.deviceName" :arrow="false"></u-cell-item>
			<u-cell-item title="设备ID" :value="deviceData.deviceId" :arrow="false"></u-cell-item>
			<!-- <u-cell-item title="所属产品" :value="" :arrow="false"></u-cell-item> -->
			<u-cell-item title="接入协议" value="MQTT" :arrow="false"></u-cell-item>
			<u-cell-item title="创建时间" :value="deviceData.createTime" :arrow="false"></u-cell-item>
		</u-cell-group>
		<view class="btn">
			<u-button type="error" shape="circle" @click="open">删除</u-button>
		</view>
		<u-modal v-model="show" :content="content" @confirm="onConfirm" ref="uModal" :async-close="true"
			:mask-close-able="true" :show-cancel-button='true' @cancel="onCancel"></u-modal>
	</view>
</template>

<script>
	const {
		request
	} = require('../../../services/request.js');
	export default {
		data() {
			return {
				deviceInfo: {},
				show: false,
				content: '要删除这个设备吗？',
				deviceData: {}
			}
		},
		onLoad() {
			this.getLastPageData();
			
		},
		methods: {
			getLastPageData() {
				const that = this;
				const eventChannel = this.getOpenerEventChannel();
				eventChannel.on('getDeviceInfo', data => {
					that.deviceInfo = data;
					that.getDeviceData()
				})
			},
			open() {
				this.show = true;
			},
			onCancel() {
				this.show = false;
			},
			async onConfirm() {
				const res = await request(
					`/aep_device_management/device?productId=${this.deviceInfo.productId}&deviceIds=${this.deviceInfo.deviceId}`,
					'DELETE', 20181031202131, {
						MasterKey: this.deviceInfo.apikey,
						productId: this.deviceInfo.productId,
						deviceIds: this.deviceInfo.deviceId
					});
				console.log(res);
				this.show = false;
				if (res.data.code === 0) {
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					})
					uni.redirectTo({
						url: '../../index/index'
					})
				} else {
					uni.showToast({
						icon: 'error',
						title: '请重试'
					})
				}

			},

			async getDeviceData() {
				const res = await request('/aep_device_management/device','GET',20181031202139,{
					MasterKey:this.deviceInfo.apikey,
					productId:this.deviceInfo.productId,
					deviceId:this.deviceInfo.deviceId
				});
				
				console.log(res)
				this.deviceData= res.data.result;
				this.deviceData.createTime = new Date(this.deviceData.createTime).toLocaleString()
			}
		}
	}
</script>

<style>
	
	.btn{
		width: 70%;
		margin: 60rpx auto;
	}

</style>
